package com.qushiyun.devops.admin.sync.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.qushiyun.devops.admin.sync.data.po.DataSourceConfig;
import com.qushiyun.devops.admin.sync.data.service.DataSourceConfigService;
import com.qushiyun.devops.common.core.web.response.ResponseResult;
import com.qushiyun.devops.common.framework.mybatis.page.vo.PageVO;
import com.qushiyun.devops.common.log.annotation.Log;
import com.qushiyun.devops.common.log.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 数据源管理
 *
 * @author zhouzx
 */
@RestController
@RequestMapping("/sync/db")
public class DataSourceConfigController {

    @Autowired
    private DataSourceConfigService dataSourceConfigService;

    @GetMapping("/page")
    @PreAuthorize("@auth.hasUrl()")
    public ResponseResult<PageVO<DataSourceConfig>> page(PageDTO pageDTO, DataSourceConfig connection) {
        PageVO<DataSourceConfig> pageVO = dataSourceConfigService.pageList(pageDTO, connection);
        return ResponseResult.success(pageVO);
    }

    @GetMapping("/list")
    @PreAuthorize("@auth.hasUrl()")
    public ResponseResult<List<DataSourceConfig>> list(Integer type) {
        LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(DataSourceConfig.class)
            .eq(type != null, DataSourceConfig::getType, type)
            .select(DataSourceConfig::getId, DataSourceConfig::getName);
        List<DataSourceConfig> list = dataSourceConfigService.list(wrapper);
        return ResponseResult.success(list);
    }

    @GetMapping("/getById/{id}")
    @PreAuthorize("@auth.hasUrl()")
    public ResponseResult<DataSourceConfig> getById(@PathVariable Long id) {
        return ResponseResult.success(dataSourceConfigService.getById(id));
    }

    @PostMapping
    @PreAuthorize("@auth.hasUrl()")
    @Log(moduleName = "数据源配置", businessType = BusinessType.ADD)
    public ResponseResult add(@Valid @RequestBody DataSourceConfig addDTO) {
        dataSourceConfigService.save(addDTO);
        return ResponseResult.success();
    }

    @PutMapping
    @ApiOperation("修改")
    @PreAuthorize("@auth.hasUrl()")
    @Log(moduleName = "数据源配置", businessType = BusinessType.UPDATE)
    public ResponseResult update(@Valid @RequestBody DataSourceConfig updateDTO) {
        dataSourceConfigService.updateById(updateDTO);
        return ResponseResult.success();
    }

    @DeleteMapping("/delete/{ids}")
    @ApiOperation("删除")
    @PreAuthorize("@auth.hasUrl()")
    @Log(moduleName = "数据源配置", businessType = BusinessType.DELETE)
    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    public ResponseResult delete(@PathVariable Long[] ids) {
        dataSourceConfigService.removeByIds(Arrays.asList(ids));
        return ResponseResult.success();
    }


}